//
//  SNavUtil.swift
//  niuguwang
//
//  Created by BrightLi on 2021/3/29.
//  Copyright © 2021年 taojinzhe. All rights reserved.
//

import Foundation
import UIKit

public let NavButtonWidth:CGFloat = ScreenWidth * 0.2
public let NavBarItemMargin:CGFloat = SFitUtil.mainScreen().bounds.size.width > 375 ? 20 : 16
public let nav_BackImage = "tools_nav_left_back"

@objcMembers open class SNavUtil:NSObject
{
    dynamic public class var navBarItemMargin:CGFloat{
        get{
            return NavBarItemMargin
        }
    }
    
    dynamic public class func leftIcon(_ target:UIViewController,firstIcon:String,secondIcon:String,gap:CGFloat
        ,onClickFirst:ChangeBlock?,onClickSecond:ChangeBlock?){
        if !SJumpUtil.isTop(target){
            return
        }
        let customView = UIView.init(frame: CGRect(x:0,y:0,width:NavButtonWidth,height:NavBarHeight))
        let first = SThemeUtil.icon(firstIcon)
        if let firstImage = first.image{
            first.frame = CGRect(x:0,y:(44-firstImage.size.height)/2
                ,width:firstImage.size.width,height:firstImage.size.height)
        }
        let firstView = UIView.init(frame: CGRect(x:0,y:0,width:NavButtonWidth,height:44))
        firstView.addSubview(first)
        customView.addSubview(firstView)
        let second = SThemeUtil.icon(secondIcon)
        if let secondImg = second.image{
            second.frame = CGRect(x:gap,y:(44-secondImg.size.height)/2
                ,width:secondImg.size.width,height:secondImg.size.height)
        }
        let w = NavButtonWidth-(first.width+gap)
        let secondView = UIView.init(frame: CGRect(x:first.width+gap,y:0,width:w,height:44))
        secondView.tag = 1000
        secondView.addSubview(second)
        customView.addSubview(secondView)
        let item=UIBarButtonItem.init(customView: customView)
        target.navigationItem.leftBarButtonItems=[item]
        firstView.s_clickBlock { (tag) in
            onClickFirst?()
        }
        secondView.s_clickBlock { (tag) in
            onClickSecond?()
        }
    }
    // 隐藏或显示关闭左侧第二个按钮
    dynamic public class func leftSecondHidden(_ target:UIViewController,hidden:Bool){
        if !SJumpUtil.isTop(target){
            return
        }
        guard let view=target.navigationItem.leftBarButtonItems?.last?.customView?.viewWithTag(1000) else{
            return
        }
        view.isHidden=hidden
    }
    
    dynamic public class func leftView(_ target:UIViewController,view:UIView)
    {
        if !SJumpUtil.isTop(target){
            return
        }
        view.frame = CGRect(x:0,y:0,width:NavButtonWidth,height:CGFloat(NavBarHeight))
        target.navigationItem.leftBarButtonItem = UIBarButtonItem(customView:view)
    }
    
    @objc class func leftIcon(_ target:UIViewController,icon:String)
    {
        if !SJumpUtil.isTop(target){
            return
        }
        let item = itemLeft(icon)
        target.navigationItem.leftBarButtonItem = item
    }
    
    dynamic public class func leftIcon(_ target:UIViewController,icon:String,onClick:@escaping (() -> ()))
    {
        if !SJumpUtil.isTop(target){
            return
        }
        let item = itemLeft(icon,onClick:onClick)
        target.navigationItem.leftBarButtonItem = item
    }
    //
    // 设置导航栏为一个图标并响应点击事件
    dynamic public class func rightIcon(_ target:UIViewController,icon:String,onClick:@escaping ChangeBlock)
    {
        if !SJumpUtil.isTop(target){
            return
        }
        let item = itemRight(icon,onClick:onClick)
        target.navigationItem.rightBarButtonItem = item
    }
    
    dynamic public class func rightIcon(_ target:UIViewController,firstIcon:String,secondIcon:String,gap:CGFloat
        ,onClickFirst:ChangeBlock?,onClickSecond:ChangeBlock?){
        if !SJumpUtil.isTop(target){
            return
        }
        let customView = UIView.init(frame: CGRect(x:0,y:0,width:NavButtonWidth,height:NavBarHeight))
        let first = SThemeUtil.icon(firstIcon)
        if let firstImage = first.image{
            first.frame = CGRect(x:0,y:(44-firstImage.size.height)/2
                ,width:firstImage.size.width,height:firstImage.size.height)
        }
        let firstView = UIView.init(frame: CGRect(x:0,y:0,width:NavButtonWidth,height:44))
        firstView.addSubview(first)
        customView.addSubview(firstView)
        let second = SThemeUtil.icon(secondIcon)
        if let secondImg = second.image{
            second.frame = CGRect(x:gap,y:(44-secondImg.size.height)/2
                ,width:secondImg.size.width,height:secondImg.size.height)
        }
        let w = NavButtonWidth-(first.width+gap)
        let secondView = UIView.init(frame: CGRect(x:first.width+gap,y:0,width:w,height:44))
        secondView.tag = 1000
        secondView.addSubview(second)
        customView.addSubview(secondView)
        let item=UIBarButtonItem.init(customView: customView)
        target.navigationItem.rightBarButtonItems=[item]
        firstView.s_clickBlock { (tag) in
            onClickFirst?()
        }
        secondView.s_clickBlock { (tag) in
            onClickSecond?()
        }
    }
    
    dynamic public class func rightView(_ target:UIViewController?,customView:UIView,onClick:@escaping ChangeBlock)
    {
        if !SJumpUtil.isTop(target){
            return
        }
        let item=UIBarButtonItem.init(customView: customView)
        target?.navigationItem.rightBarButtonItem = item
        customView.s_clickBlock { (tap) in
            onClick()
        }
    }
    
    dynamic public class func itemLeft(_ icon:String) -> UIBarButtonItem
    {
        let customView = UIView(frame:CGRect(x:0,y:0,width:NavButtonWidth,height:CGFloat(NavBarHeight)))
        if let image = UIImage(named:icon){
            let leftIcon:UIImageView = UIImageView(image: image)
            leftIcon.tag=100
            customView.addSubview(leftIcon)
            leftIcon.frame=CGRect(x:0,y:(CGFloat(NavBarHeight)-image.size.height)/2,width:image.size.width,height:image.size.height)
        }
        return UIBarButtonItem(customView: customView)
    }
    
    dynamic public class func itemLeft(_ icon:String,onClick:@escaping (() -> ())) -> UIBarButtonItem
    {
        let customView = UIView(frame:CGRect(x:0,y:0,width:NavButtonWidth,height:CGFloat(NavBarHeight)))
        if let image = UIImage(named:icon){
            let leftIcon:UIImageView = UIImageView(image: image)
            leftIcon.tag=100
            customView.addSubview(leftIcon)
            leftIcon.frame=CGRect(x:0,y:(CGFloat(NavBarHeight)-image.size.height)/2,width:image.size.width,height:image.size.height)
        }
        customView.s_clickBlock { (tap:UITapGestureRecognizer) in
            onClick()
        }
        return UIBarButtonItem(customView: customView)
    }
    
    dynamic public class func itemRight(_ icon:String,onClick:@escaping ChangeBlock) -> UIBarButtonItem
    {
        let customView = UIView(frame:CGRect(x:0,y:0,width:NavButtonWidth,height:CGFloat(NavBarHeight)))
        if let image = UIImage(named:icon){
            let rightIcon:UIImageView = UIImageView(image: image)
            rightIcon.tag=100
            customView.addSubview(rightIcon)
            rightIcon.frame=CGRect(x:NavButtonWidth-image.size.width,y:(CGFloat(NavBarHeight)-image.size.height)/2,width:image.size.width,height:image.size.height)
        }
        customView.s_clickBlock { (tap:UITapGestureRecognizer) in
            onClick()
        }
        return UIBarButtonItem(customView: customView)
    }
    
    dynamic public class func leftBack(_ target:UIViewController,icon:String)
    {
        weak var weakVC = target
        SNavUtil.leftIcon(target, icon:icon) {
            if let _ = weakVC?.navigationController?.popViewController(animated: true){
            }else{
                weakVC?.dismiss(animated:true){() -> Void in}
            }
        }
    }
    
    
    dynamic public class func leftBack(_ target:UIViewController)
    {
        if !SJumpUtil.isTop(target){
            return
        }
        weak var weakVC = target
        SNavUtil.leftIcon(target, icon:nav_BackImage) {
            if let _ = weakVC?.navigationController?.popViewController(animated: true){
            }else{
                weakVC?.dismiss(animated:true){() -> Void in}
            }
        }
    }
    
    dynamic public class func theme(_ target:UIViewController,title:String){
        if !SJumpUtil.isTop(target){
            return
        }
        weak var weakVC = target
        leftIcon(target, icon:nav_BackImage) {
            if let _ = weakVC?.navigationController?.popViewController(animated: true){
            }else{
                weakVC?.dismiss(animated:true){() -> Void in}
            }
        }
        if let titleView:YYNavTitle = target.navigationItem.titleView as? YYNavTitle{
            titleView.title.text = title
        }else{
            let titleView = YYNavTitle(frame:NavTitleRect)
            titleView.title.text = title
            target.navigationItem.titleView = titleView
        }
        if let navigationBar = SJumpUtil.topVC()?.navigationController?.navigationBar{
            navigationBar.barTintColor = SUIUtil.colorOf(0xffffff)
            navigationBar.isTranslucent = false
            navigationBar.shadowImage=UIImage.imageWithColor(SUIUtil.colorOf(0xeeeeee),width:1,height:LineHeight)
        }
    }
    
   
    dynamic public class func theme(_ target:UIViewController,title:String,onBack:ChangeBlock?)
    {
        if !SJumpUtil.isTop(target){
            return
        }
        leftIcon(target, icon: nav_BackImage){
            onBack?()
        }
        if let titleView:YYNavTitle = target.navigationItem.titleView as? YYNavTitle{
            titleView.title.text = title
        }else{
            let titleView = YYNavTitle(frame:NavTitleRect)
            titleView.title.text = title
            target.navigationItem.titleView = titleView
        }
        if let navigationBar = SJumpUtil.topVC()?.navigationController?.navigationBar{
            navigationBar.barTintColor = SUIUtil.colorOf(0xffffff)
            navigationBar.isTranslucent = false
            navigationBar.shadowImage=UIImage.imageWithColor(SUIUtil.colorOf(0xeeeeee),width:1,height:LineHeight)
        }
    }
    
    dynamic public class func theme(_ target:UIViewController,titleView:UIView){
        if !SJumpUtil.isTop(target){
            return
        }
        weak var weakVC = target
        leftIcon(target, icon:nav_BackImage) {
            if let _ = weakVC?.navigationController?.popViewController(animated: true){
            }else{
                weakVC?.dismiss(animated:true){() -> Void in}
            }
        }
        titleView.frame=CGRect(x:0,y:0,width:ScreenWidth*0.5,height:NavBarHeight)
        target.navigationItem.titleView = titleView
        if let navigationBar = SJumpUtil.topVC()?.navigationController?.navigationBar{
            navigationBar.barTintColor = SUIUtil.colorOf(0xffffff)
            navigationBar.isTranslucent = false
            navigationBar.shadowImage=UIImage.imageWithColor(SUIUtil.colorOf(0xeeeeee),width:1,height:0.5)
        }
    }
    // 设置导航栏返回标题与右侧图标与点击事件
    dynamic public class func theme(_ target:UIViewController,title:String,rightIcon:String,onRightClick:@escaping ChangeBlock)
    {
        if !SJumpUtil.isTop(target){
            return
        }
        weak var weakVC = target
        leftIcon(target, icon:nav_BackImage) {
            if let _ = weakVC?.navigationController?.popViewController(animated: true){
            }else{
                weakVC?.dismiss(animated:true){() -> Void in}
            }
        }
        if let titleView:YYNavTitle = target.navigationItem.titleView as? YYNavTitle{
            titleView.title.text = title
        }else{
            let titleView = YYNavTitle(frame:NavTitleRect)
            titleView.title.text = title
            target.navigationItem.titleView = titleView
        }
        if let navigationBar = SJumpUtil.topVC()?.navigationController?.navigationBar{
            navigationBar.barTintColor = SUIUtil.colorOf(0xffffff)
            navigationBar.isTranslucent = false
            navigationBar.shadowImage=UIImage.imageWithColor(SUIUtil.colorOf(0xeeeeee),width:1,height:0.5)
        }
        SNavUtil.rightIcon(target, icon:rightIcon, onClick: onRightClick)
    }
    
    // 设置导航栏返回标题与右侧标题与点击事件
    dynamic public class func theme(_ target:UIViewController,title:String,rightTitle:String,onRightClick:@escaping ChangeBlock)
    {
        if !SJumpUtil.isTop(target){
            return
        }
        weak var weakVC = target
        leftIcon(target, icon:nav_BackImage) {
            if let _ = weakVC?.navigationController?.popViewController(animated: true){
            }else{
                weakVC?.dismiss(animated:true){() -> Void in}
            }
        }
        if let titleView:YYNavTitle = target.navigationItem.titleView as? YYNavTitle{
            titleView.title.text = title
        }else{
            let titleView = YYNavTitle(frame:NavTitleRect)
            titleView.title.text = title
            target.navigationItem.titleView = titleView
        }
        if let navigationBar = SJumpUtil.topVC()?.navigationController?.navigationBar{
            navigationBar.barTintColor = SUIUtil.colorOf(0xffffff)
            navigationBar.isTranslucent = false
            navigationBar.shadowImage=UIImage.imageWithColor(SUIUtil.colorOf(0xeeeeee),width:1,height:0.5)
        }
        SNavUtil.rightTitle(target, title: rightTitle, color: 0x3875F6, onClick: onRightClick)
    }
    
    // 获得导航栏当前设置的文本
    dynamic public class func titleOf(_ target:UIViewController) -> String
    {
        if !SJumpUtil.isTop(target){
            return ""
        }
        if let titleView:YYNavTitle = target.navigationItem.titleView as? YYNavTitle{
            return titleView.title.text ?? ""
        }else{
            return ""
        }
    }
    // 设置导航栏标题为一个视图
    dynamic public class func resetTitle(_ target:UIViewController,view:UIView)
    {
        if !SJumpUtil.isTop(target){
            return
        }
        view.frame = CGRect(x:0,y:0,width:ScreenWidth*0.5,height:NavBarHeight)
        target.navigationItem.titleView = view
    }
    // 设置导航栏标题为一个文本
    dynamic public class func resetTitle(_ target:UIViewController,title:String)
    {
        resetTitle(target,title:title,color:nav_TitleColor)
    }
    // 设置导航栏标题为一个文本并设置文本颜色
    dynamic public class func resetTitle(_ target:UIViewController, title:String, color:UInt32)
    {
        if !SJumpUtil.isTop(target){
            return
        }
        var titleView:YYNavTitle? = target.navigationItem.titleView as? YYNavTitle
        if titleView == nil
        {
            titleView = YYNavTitle.init(frame: CGRect(x:0,y:0,width:ScreenWidth*0.5,height:NavBarHeight))
            target.navigationItem.titleView = titleView!
        }
        titleView!.title.text = title
        titleView!.title.textColor = SUIUtil.colorOf(color)
    }
    dynamic public class func liveSingleResetTitle(_ target:UIViewController, title:String, color:UInt32)
    {
        
        var titleView:YYNavTitle? = target.navigationItem.titleView as? YYNavTitle
        if titleView == nil
        {
            titleView = YYNavTitle.init(frame: CGRect(x:0,y:0,width:ScreenWidth*0.5,height:NavBarHeight))
            target.navigationItem.titleView = titleView!
        }
        titleView!.title.text = title
        titleView!.title.textColor = SUIUtil.colorOf(color)
    }
    // 设置导航栏右侧为一个指定颜色的文本并响应点击
    @discardableResult
    dynamic public class func rightTitle(_ target:UIViewController,title:String,color:UInt32,onClick:@escaping ChangeBlock)->UILabel?
    {
        if !SJumpUtil.isTop(target){
            return nil
        }
        let customView:UIView = UIView(frame:CGRect(x:0,y:0,width:NavButtonWidth,height:NavBarHeight))
        let label = UILabel(frame:customView.frame);
        label.text = title
        label.textColor = SUIUtil.colorOf(color)
        label.font = SFitUtil.fitFont(14)
        label.adjustsFontSizeToFitWidth = true
        label.minimumScaleFactor = 0.5
        label.textAlignment = .right
        customView.addSubview(label)
        customView.s_clickBlock { (tap:UITapGestureRecognizer) in
            onClick()
        }
        target.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: customView)
        return label
    }
    dynamic public class func rightTitle(_ target:UIViewController,title:String,color:UInt32,font:CGFloat,onClick:@escaping ChangeBlock)
    {
        let customView:UIView = UIView(frame:CGRect(x:0,y:0,width:NavButtonWidth,height:NavBarHeight))
        let label = UILabel(frame:customView.frame);
        label.text = title
        label.textColor = SUIUtil.colorOf(color)
        label.font = SFitUtil.fitFont(font)
        label.adjustsFontSizeToFitWidth = true
        label.minimumScaleFactor = 0.5
        label.textAlignment = .right
        customView.addSubview(label)
        customView.s_clickBlock { (tap:UITapGestureRecognizer) in
            onClick()
        }
        target.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: customView)
    }
    dynamic public class func clearRight(_ target:UIViewController){
        if !SJumpUtil.isTop(target){
            return
        }
        target.navigationItem.rightBarButtonItem = nil
    }
    
    dynamic public class func right(_ target:UIViewController,view:UIView)
    {
        if !SJumpUtil.isTop(target){
            return
        }
        target.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: view)
    }

}
